package cn.itcast.demo;

import cn.itcast.demo.pojo.Heima63;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.junit.Test;

import java.util.List;

public class SolrJQueryIndex {


    @Test
    public void testIndexQuery() throws SolrServerException {
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");


        //查询条件对象
        SolrQuery solrQuery = new SolrQuery("title:手机");

        //执行查询，传入查询条件对象,执行查询（发起web请求，），得到响应结果
        QueryResponse response = solrServer.query(solrQuery);

        //从响应的对象中解析结果数据
        SolrDocumentList results = response.getResults();


        results.forEach(result->{
            String id = (String) result.get("id");
            String title = (String) result.get("title");
            Long price = (Long) result.get("price");

            System.out.println("id:"+id+" title:"+title+" price:"+price);
        });
    }



    @Test
    public void testIndexQueryWithBean() throws SolrServerException {
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");


        //查询条件对象
        SolrQuery solrQuery = new SolrQuery("title:手机");

        //执行查询，传入查询条件对象,执行查询（发起web请求，），得到响应结果
        QueryResponse response = solrServer.query(solrQuery);

        //从响应的对象中解析结果数据

        //从响应结果中解析数据，并把数据封装到Heima63的对象中
        List<Heima63> beans = response.getBeans(Heima63.class);

        beans.forEach(bean->{
            String id = bean.getId();

            String title = bean.getTitle();

            Long price = bean.getPrice();

            System.out.println("id:"+id+" title:"+title+" price:"+price);
        });
    }


}
